package xupt.com.bolg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xupt.com.bolg.entity.User;
import xupt.com.bolg.service.impl.UserServiceImpl;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.util.Properties;

@Controller
public class ResetPassword {
    @Autowired
    UserServiceImpl userService;
    @GetMapping("/findPassword")
    public String skip(){
        return "wangjimima";
    }
    @PostMapping("/sendEmailCode")
    public String sendCode(HttpServletRequest request,String email) throws MessagingException {
        request.setAttribute("email",email);
        //发件人地址
         String senderAddress = "lt847564846@163.com";
        //收件人地址
         String recipientAddress = email;
        //发件人授权码
         String senderPassword = "123456789lt";
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth","true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host","smtp.163.com");// smtp服务器地址
        Session session = Session.getInstance(props);
        session.setDebug(true);
        Message msg = new MimeMessage(session);
        msg.setSubject("Bolg验证码");
        double random1 = Math.random()*1000000;
        int random=(int)random1;
        System.out.println(random);
        msg.setText("您正在进行更改密码操作，如果不是本人请注意账号安全。请不要告诉别人。验证码:"+random);
        msg.setFrom(new InternetAddress(senderAddress));//发件人邮箱(我的163邮箱)

        msg.setRecipient(Message.RecipientType.TO,
                new InternetAddress(recipientAddress)); //收件人邮箱(我的QQ邮箱)
        //给自己也发一份
        msg.setRecipient(Message.RecipientType.TO,
                new InternetAddress(senderAddress)); //收件人邮箱(我的QQ邮箱)
        msg.saveChanges();
        Transport transport = session.getTransport();
        transport.connect(senderAddress,senderPassword);//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
        request.getSession().setAttribute("random",random);//将验证码写进Session
        request.getSession().setAttribute("email",email);//将email 写入Session中为下一次找用户做准备
        return "wangjimima";
    }

    @PostMapping("/checkCode")
    public String checkCode(@RequestParam(name = "code")String code,HttpServletRequest request){
       Integer random =(Integer) request.getSession().getAttribute("random");
        String s = random.toString();
        //忽略大小写比较
        if(s.equalsIgnoreCase(code)){
            System.out.println("验证正确");
            User email = userService.findByEmail((String) request.getSession().getAttribute("email"));
            request.setAttribute("name",email.getName());
            return "wangjimima2";
        }
        request.setAttribute("msg","验证码错误，请重试");
        return "wangjimima";
    }
    @PostMapping("/resetPassword")
    public String rest(@RequestParam(name = "pass1") String pass1,HttpServletRequest request){
        String email =(String) request.getSession().getAttribute("email");
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"+email);
        User byEmail = userService.findByEmail(email);
        if(email==null){
            request.setAttribute("msg","没有这个用户");
            return "erro";
        }
        System.out.println("修改前**********************"+byEmail.toString());
        byEmail.setPassword(pass1);

        boolean update = userService.update(byEmail);
        if(update){
            request.getSession().removeAttribute("email");
            request.getSession().setAttribute("name",byEmail.getName());
            return "homepage";
        }
        request.setAttribute("msg","更改失败，没有此用户");
       return "wangjimima";
    }
}
